<?php
class IndexAction extends CommonAction
{
	/**
	 +----------------------------------------------------------
	 * 默认操作
	 +----------------------------------------------------------
	 */
	public function index()
	{
		
		$bestSellers = $this->getBestSellers();
		$top15NewProducts = $this->getTop15ForNewProducts();
		$allCategories = $this->getAllCategories();
		$newOrders = $this->getNewOrders();
		$featuredCategories = $this->getFeaturedCategories();

		$this->assign("bestSellers",$bestSellers);
		$this->assign("top15NewProducts",$top15NewProducts);
		$this->assign("allCategories",$allCategories);
		$this->assign("newOrders",$newOrders);
		$this->assign("featuredCategories",$featuredCategories);
		$this->display();
	}

	protected function getBestSellers(){
		$Product = D("Product");
		$products = $Product->where("best_sellers = 1")->order("best_sellers_order_key desc")->limit(5)->select();
		return $products;
	}

	protected function getTop15ForNewProducts(){
		$Product = D("Product");
		$products = $Product->order("create_time desc")->limit(15)->select();
		return $products;
	}

	protected function getNewOrders(){
		$orderModel = D("Order");
		$newOrders = $orderModel->relation(true)->order("create_time desc")->limit(20)->select();
		return $newOrders;
	}

	protected function getFeaturedCategories(){
		$categoryModel = D("Category");
		$featuredCategories = $categoryModel->relation(true)->where("featured=1 AND parent=0")->order("order_key desc")->limit(3)->select();

		for ($i = 0;$i < count($featuredCategories); $i++) {
			$featuredCategoryModel = D("Category");
			$featuredCategories[$i]["featuredSubCategories"] = $featuredCategoryModel->where("parent=".$featuredCategories[$i]["id"] . " AND featured=1")->order("order_key desc")->limit(5)->select();
		}
		return $featuredCategories;
	}

}
?>